﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MangaRipper.Core
{
    [Serializable]
    class TitleMangaEden : TitleBase
    {
        public TitleMangaEden(Uri address) : base(address) { }

        protected override List<IChapter> ParseChapterObjects(string html)
        {
            var list = new List<IChapter>();
            Regex reg = new Regex(@"<a href=""(?<Value>[^\""]+?)"" class=""chapterLink"">(\r?\n)(.*)<span class=""chapterDate"">(?<Text>[^<]+?)</span>(\r?\n)(.*)<b>(?<Chap>[^<]*?)</b>",
                RegexOptions.IgnoreCase | RegexOptions.Multiline);
            MatchCollection matches = reg.Matches(html);

            foreach (Match match in matches)
            {
                string[] Chapter = new string[1];
                //string addr = match.Groups["Value"].Value.TrimEnd(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
                var value = new Uri(Address, match.Groups["Value"].Value);
                if (match.Groups["Chap"].Value.Contains(':')) Chapter = match.Groups["Chap"].Value.Split(':');
                else Chapter[0] = match.Groups["Chap"].Value;
                string name = match.Groups["Text"].Value + " " + Chapter[0];

                var recentItem = list.Where(c => c.Address == value).FirstOrDefault();

                if (recentItem != null)
                {
                    list.Remove(recentItem);
                }

                IChapter chapter = new ChapterMangaEden(name, value);
                list.Add(chapter);
            }

            return list;
        }
    }
}
